package com.qk.manangement.controller;

import com.qk.common.properties.RedisProperties;
import com.qk.common.response.BusinessGetByIdResponse;
import com.qk.common.response.BusinessUpdateResponse;
import com.qk.common.response.PageResult;
import com.qk.common.response.Result;
import com.qk.entity.pojo.Business;
import com.qk.entity.request.business.BusinessPageRequest;
import com.qk.entity.request.business.BusinessPoolRequest;
import com.qk.manangement.annotation.ClearRedisCache;
import com.qk.manangement.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 商机表数据查询分页
     */
    @GetMapping
    public Result queryBusinesses(BusinessPageRequest businessPageRequest) {
        return Result.success(businessService.pageList(businessPageRequest));
    }

    /**
     * 新增商机
     */
    @ClearRedisCache(keys = {RedisProperties.REDIS_KEY_PREFIX+RedisProperties.REDIS_KEY_OVERVIEW})
    @PostMapping
    public Result add(@RequestBody Business business) {
        boolean add = businessService.add(business);
        if (add) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 分配商机
     */
    @PutMapping("/assign/{businessId}/{userId}")
    @ClearRedisCache(keys = {RedisProperties.REDIS_KEY_PREFIX+RedisProperties.REDIS_KEY_OVERVIEW})
    public Result assign(@PathVariable Long businessId, @PathVariable Long userId) {
        boolean assign = businessService.assign(businessId, userId);
        if (assign) {
            return Result.success("分配成功");
        }
        return Result.error("分配失败");
    }

    /**
     * 踢回公海
     */
    @PutMapping("/back/{id}")
    @ClearRedisCache(keys = {RedisProperties.REDIS_KEY_PREFIX+RedisProperties.REDIS_KEY_OVERVIEW})
    public Result back(@PathVariable Long id) {
        boolean back = businessService.back(id);
        if (back) {
            return Result.success("踢回成功");
        }
        return Result.error("踢回失败");
    }

    /**
     * 转客户处理
     */
    @PostMapping("/toCustomer/{id}")
    @ClearRedisCache(keys = {RedisProperties.REDIS_KEY_PREFIX+RedisProperties.REDIS_KEY_OVERVIEW})
    public Result toCustomer(@PathVariable Integer id) {
        boolean assign = businessService.toCustomer(id);
        if (assign) {
            return Result.success("转客户成功");
        }
        return Result.error("转客户失败");
    }

    /**
     * 根据id查询商机
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        BusinessGetByIdResponse business = businessService.getBusinessById(id);

        return Result.success(business);

    }

    /**
     * 跟进商机
     */
    @PutMapping
    @ClearRedisCache(keys = {RedisProperties.REDIS_KEY_PREFIX+RedisProperties.REDIS_KEY_OVERVIEW})
    public Result update(@RequestBody BusinessUpdateResponse  business) {
        boolean update = businessService.businessUpdate(business);
        if (update) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    /**
     * 公海池列表查询
     */
    @GetMapping("/pool")
    public Result poolList(BusinessPoolRequest businessPoolRequest) {
        PageResult pageResult = businessService.businessPoolList(businessPoolRequest);
        return Result.success(pageResult);
    }


}
